<?php

/**
 *  @FileComment            [商品管理控制器]
 *  @ProjectName             项目名称：shopFrame
 *  @FileName                   文件名称：GoodsController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-9 11:46:52
 *  *
 */
class GoodsController extends CommonController {

        private $model;

        public function __init() {
                parent::__init();
                $this->model = K('Goods');
        }

        public function index() {
                //显示商品信息
                $goods = $this->model->all();
                $this->assign('goods', $goods);
                $this->display();
        }

        public function add() {
                //添加商品
                if (IS_POST) {
                        if ($this->model->create()) {
                                $this->model->add_data();
                                $this->success('添加商品成功！', U('index'));
                        } else {
                                $this->error($this->model->error);
                        }
                }
                //获取所有类型
                $typeInfo = K('Type')->get_all();
                $this->assign('typeInfo', $typeInfo);
                //获取所有品牌
                $brandInfo = K('Brand')->get_data();
                $this->assign('brandInfo', $brandInfo);
                $this->display();
        }

        public function edit() {
                //编辑商品
                if (IS_POST) {
                        if ($this->model->create()) {
                                $this->model->edit_data();
                                $this->success('商品修改成功！', U('index'));
                        } else {
                                $this->error($this->model->error);
                        }
                }
                $tid = Q('get.tid', 0, 'intval');
                //获取所有类型
                $typeInfo = K('Type')->get_all();
                $this->assign('typeInfo', $typeInfo);
                //获取所有品牌
                $brandInfo = K('Brand')->get_data($tid);
                $this->assign('brandInfo', $brandInfo);
                //获得当前商品信息
                $gid = Q('get.gid', 0, 'intval');
                $goodsInfo = K('Goods')->get_one_data($gid);
                $this->assign('goodsInfo', $goodsInfo);
                //获得当前分类的信息
                $cateInfo = K('Category')->get_tree_data($tid);
                $this->assign('cateInfo', $cateInfo);
                //获得当前类型的所有属性
                $attrList = K('TypeAttr')->get_class_data($tid, 0);
                foreach ($attrList as $k => $v) {
                        $attrList[$k]['tavalue'] = explode(',', $v['tavalue']);
                }
                $this->assign('attrList', $attrList);
                //获得当前类型的所有规格
                $standList = K('TypeAttr')->get_class_data($tid, 1);
                foreach ($standList as $k => $v) {
                        $standList[$k]['tavalue'] = explode(',', $v['tavalue']);
                }
                $this->assign('standList', $standList);
                //获得图片信息
                $listImg = array(
                    0 => array(
                        'path' => $goodsInfo['listimg']
                    )
                );
                $this->assign('listimg', $listImg);
                $goodsdetail = K('GoodsDetail')->get_one_data($gid);
                $thumb = unserialize($goodsdetail['thumb']);
                $goodsAttr = K('GoodsAttrTypeAttrView')->get_data($gid, 0);
                $this->assign('goodsAttr', $goodsAttr);
                //商品属性
                $goodStand = K('GoodsAttrTypeAttrView')->get_data($gid, 1);
                $this->assign('goodStand', $goodStand);
                //商品规格
                $this->assign('goodsdetail', $goodsdetail);
                $this->assign('thumb', $thumb);
                $this->display();
        }

        public function del() {
                //删除商品
                $gid = Q('get.gid', 0, 'intval');
                $this->model->del_data($gid);
                $this->success('删除商品成功！', U('index'));
        }

}
